package com.analyse.stock.controller;

import com.analyse.stock.service.IQuartzService;
import com.analyse.stock.vo.JobInfo;
import com.analyse.stock.vo.ResultUtils;
import com.analyse.stock.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/quartz")
public class QuartzController {

    @Autowired
    private IQuartzService quartzService;
    
    /**
     * 手动创建一个job
     * {
     *     "jobName":"选股_每日板块净流入资金前5名",
     *     "cron":"0 00 16 1/1 * ?",
     *     "jobClassName":"com.analyse.stock.job.ScanStockDailyJob"
     * }
     **/
    @PostMapping("/createJob")
    public ResultVo<String> createJob(@RequestBody JobInfo jobInfo) {
        String result = quartzService.addCronJob(jobInfo.getJobName(), jobInfo.getCron(),jobInfo.getJobClassName());
        return ResultUtils.ok(result);
    }
    
    /**
     * 删除一个job
     * {
     *     "jobName":"每日增量15时后执行"
     * }
     **/
    @PostMapping("/deleteJob")
    public ResultVo<String> deleteJob(@RequestBody JobInfo jobInfo) {
        String result = quartzService.deleteCronJob(jobInfo.getJobName());
        return ResultUtils.ok(result);
    }

    /**
     * 立即执行一次job
     * {
     *     "jobName":"选股_每日板块净流入资金前5名",
     *     "jobClassName":"com.analyse.stock.job.ScanStockDailyJob"
     * }
     **/
    @PostMapping("/executeImmediately")
    public ResultVo<String> executeImmediately(@RequestBody JobInfo jobInfo) {
        String result = quartzService.executeImmediately(jobInfo.getJobName(),jobInfo.getJobClassName());
        return ResultUtils.ok(result);
    }

    /**
     * 恢复定时任务
     * {
     *     "jobName":"选股_每日板块净流入资金前5名"
     * }
     **/
    @PostMapping("/resumeTrigger")
    public ResultVo<String> resumeTrigger(@RequestBody JobInfo jobInfo) {
        String result = quartzService.resumeTrigger(jobInfo.getJobName());
        return ResultUtils.ok(result);
    }

    /**
     * 暂停定时任务
     * {
     *     "jobName":"选股_每日板块净流入资金前5名"
     * }
     **/
    @PostMapping("/pauseTrigger")
    public ResultVo<String> pauseTrigger(@RequestBody JobInfo jobInfo) {
        String result = quartzService.resumeTrigger(jobInfo.getJobName());
        return ResultUtils.ok(result);
    }
}
